package com.aos.eap.activity;

import android.os.Handler;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aos.eap.R;
import com.aos.eap.adapter.RecipeSearchListAdapter;
import com.aos.eap.base.BaseDialogActivity;
import com.aos.eap.base.Constant;
import com.aos.eap.bean.DownRecipeBean;
import com.aos.eap.bean.EqpTypeBean;
import com.aos.eap.bean.PeDownRecipeBean;
import com.aos.eap.bean.PkgBean;
import com.aos.eap.bean.SearchRecipeBean;
import com.aos.eap.bean.WireDiamBean;
import com.aos.eap.bean.WireTypeBean;
import com.aos.eap.bean.net.BaseResponseBean;
import com.aos.eap.utils.MyUtils;
import com.aos.eap.utils.NetUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.widget.AppCompatAutoCompleteTextView;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;

import static com.aos.eap.utils.AnimationUtils.arrowAnimation;

public class PE_DownRecipeActivity extends BaseDialogActivity {


    @BindView(R.id.tvUser)
    TextView tvUser;
    @BindView(R.id.etEqpId)
    FreeUI_ClearEditText etEqpId;
    @BindView(R.id.tvCurrentType)
    TextView tvCurrentType;
    @BindView(R.id.tvEqpType)
    AppCompatAutoCompleteTextView tvEqpType;
    @BindView(R.id.ivEqpTypeArrow)
    ImageView ivEqpTypeArrow;
    @BindView(R.id.flEqpType)
    FrameLayout flEqpType;
    @BindView(R.id.tvWireType)
    AppCompatAutoCompleteTextView tvWireType;
    @BindView(R.id.ivWireTypeArrow)
    ImageView ivWireTypeArrow;
    @BindView(R.id.flWireType)
    FrameLayout flWireType;
    @BindView(R.id.tvWireLong)
    AppCompatAutoCompleteTextView tvWireLong;
    @BindView(R.id.ivWireLongArrow)
    ImageView ivWireLongArrow;
    @BindView(R.id.flWireLong)
    FrameLayout flWireLong;
    @BindView(R.id.tvPkg)
    AppCompatAutoCompleteTextView tvPkg;
    @BindView(R.id.ivPkgArrow)
    ImageView ivPkgArrow;
    @BindView(R.id.flPkg)
    FrameLayout flPkg;
    @BindView(R.id.etRecipeId)
    FreeUI_ClearEditText etRecipeId;
    @BindView(R.id.flRecipeId)
    FrameLayout flRecipeId;
    @BindView(R.id.etRemark)
    FreeUI_ClearEditText etRemark;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.ivInfoArrow)
    ImageView ivInfoArrow;
    @BindView(R.id.llInfoTitle)
    LinearLayout llInfoTitle;
    @BindView(R.id.tvRecipeType)
    TextView tvRecipeType;
    @BindView(R.id.llInfo)
    LinearLayout llInfo;
    @BindView(R.id.tvSearch)
    TextView tvSearch;
    @BindView(R.id.llSearch)
    LinearLayout llSearch;
    @BindView(R.id.rvRecipeList)
    RecyclerView rvRecipeList;

    private PeDownRecipeBean peDownRecipeBean;
    private FreeUI_GeneralPop eqpTypePop, wireTypePop, wireLengthPop, pkgPop;
    private RecipeSearchListAdapter recipeSearchListAdapter;
    private String method = "MHS";
    private List<String> eqpTypeList,wireTypeList,wireDiamList,peDownList;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_pe_down_recipe;
    }

    @Override
    protected void initView() {
        tvUser.setText(Constant.CURRENT_USER);
        MyUtils.setEditTextListener(etEqpId, this, this::requestInfoByEqpId);

        rvRecipeList.setLayoutManager(new LinearLayoutManager(PE_DownRecipeActivity.this));
        rvRecipeList.setItemAnimator(new DefaultItemAnimator());
        rvRecipeList.addItemDecoration(new DividerItemDecoration(PE_DownRecipeActivity.this, 1));
        recipeSearchListAdapter = new RecipeSearchListAdapter(null);
        recipeSearchListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        recipeSearchListAdapter.isFirstOnly(true);
        rvRecipeList.setAdapter(recipeSearchListAdapter);
    }

    private void requestInfoByEqpId(String eqpId) {
        JsonObject map = new JsonObject();
        map.addProperty("EqpID", eqpId);
        NetUtils.requestGetNet(this, "EMS_DownRecipePELoad", map, new FreeUI_DialogEntityCallBack<BaseResponseBean<PeDownRecipeBean>>(
                new TypeToken<BaseResponseBean<PeDownRecipeBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<BaseResponseBean<PeDownRecipeBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true,getSupportFragmentManager())) {
                    peDownRecipeBean = response.body().getResponse().getBody();
                    setPageData(peDownRecipeBean);
                }else {
                    peDownRecipeBean = null;
                    setPageData(new PeDownRecipeBean());
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<PeDownRecipeBean>> response) {
                super.onError(response);
                uploadLog(response.getException());
                loadError(response.getException(), "EMS_DownRecipePELoad");
                peDownRecipeBean = null;
                setPageData(new PeDownRecipeBean());
            }
        });
    }
    private void setPageData(PeDownRecipeBean peDownRecipeBean) {
        tvCurrentType.setText(peDownRecipeBean.getEqpType());
        llSearch.setVisibility(View.VISIBLE);
        eqpTypeList = new ArrayList<>();
        for (EqpTypeBean entity:peDownRecipeBean.getEqpTypeList())
        {
            eqpTypeList.add(entity.getEqptype());
        }
        wireTypeList = new ArrayList<>();
        for (WireTypeBean entity:peDownRecipeBean.getWireTypeList())
        {
            wireTypeList.add(entity.getWiretype());
        }
        wireDiamList = new ArrayList<>();
        for (WireDiamBean entity:peDownRecipeBean.getWireDiamList())
        {
            wireDiamList.add(entity.getWirediam());
        }
        peDownList = new ArrayList<>();
        for (PkgBean entity:peDownRecipeBean.getPkgList())
        {
            peDownList.add(entity.getPkg());
        }
        tvEqpType.setAdapter(new ArrayAdapter<>(PE_DownRecipeActivity.this, android.R.layout.simple_list_item_1, peDownRecipeBean.getEqpTypeList()));

        tvWireType.setAdapter(new ArrayAdapter<>(PE_DownRecipeActivity.this, android.R.layout.simple_list_item_1, peDownRecipeBean.getWireTypeList()));

        tvWireLong.setAdapter(new ArrayAdapter<>(PE_DownRecipeActivity.this, android.R.layout.simple_list_item_1, peDownRecipeBean.getWireDiamList()));

        tvPkg.setAdapter(new ArrayAdapter<>(PE_DownRecipeActivity.this, android.R.layout.simple_list_item_1, peDownRecipeBean.getPkgList()));
    }
    @Override
    protected void initData() {

    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("PE下载Recipe");
        freeUI_titleView.setLeftListener(view -> showBackDialog(PE_DownRecipeActivity.this));
        freeUI_titleView.setRightListener("下载", v -> {
            if (MyUtils.isFastClick()){
                showToast("您点击的太快了",false);
                return;
            }
            if (peDownRecipeBean == null) {
                showToast("未获取到页面信息，请重新扫描设备编号", false);
                etEqpId.setText("");
                MyUtils.setEditTextFocus(etEqpId);
                return;
            }
            DownRecipeBean selectBean = null;
            for (DownRecipeBean downRecipeBean : recipeSearchListAdapter.getData()) {
                if (downRecipeBean.isSelect()) {
                    selectBean = downRecipeBean;
                }
            }
            if (selectBean != null) {
                JsonObject map = new JsonObject();
                map.addProperty("EqpID", peDownRecipeBean.getEqpID());
                map.addProperty("EqpType", peDownRecipeBean.getEqpType());
                map.addProperty("WbLoadRcpType", method);
                map.addProperty("RecipeType", "GR");
                map.addProperty("RecipeID", selectBean.getRecipeID());
                map.addProperty("RecipeVersion", selectBean.getRecipeVersion());
                requestSubmit(map);
            } else {
                showToast("请选择要下载的Recipe", false);
            }
        });
        return new FreeUI_AddViewEntity("header", llMain);
    }

    private void requestSubmit(JsonObject map) {
        NetUtils.requestGetNet(this, "EMS_DownRecipe", map, new FreeUI_DialogEntityCallBack<BaseResponseBean<?>>(
                new TypeToken<BaseResponseBean<?>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<BaseResponseBean<?>> response) {
                if (response.body().isSuccess(getApplicationContext(), false,getSupportFragmentManager())) {
                    showToast("下载成功", true);
                    new Handler().postDelayed(PE_DownRecipeActivity.this::finish, 2000);
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<?>> response) {
                super.onError(response);
                uploadLog(response.getException());
                loadError(response.getException(), "EMS_DownRecipe");
            }
        });
    }


    @OnClick({R.id.rbMhs,R.id.rbRam,R.id.tvSearch, R.id.llInfoTitle,R.id.ivEqpTypeArrow,R.id.ivWireTypeArrow,R.id.ivWireLongArrow,R.id.ivPkgArrow})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rbMhs:
                method = "MHS";
                break;
            case R.id.rbRam:
                method = "RAM";
                break;
            case R.id.tvSearch:
                String eqpType = tvEqpType.getText().toString().trim();
                String wireType = tvWireType.getText().toString().trim();
                String wireLong = tvWireLong.getText().toString().trim();
                String pkg = tvPkg.getText().toString().trim();
                if (FreeApi_StringUtils.isEmpty(eqpType) && FreeApi_StringUtils.isEmpty(wireType)
                        && FreeApi_StringUtils.isEmpty(pkg) && FreeApi_StringUtils.isEmpty(wireLong)) {
                    showToast("请选择至少一项查询条件", false);
                    return;
                }
                JsonObject map = new JsonObject();
                map.addProperty("EqpType", eqpType);
                map.addProperty("WireDiam", wireLong);
                map.addProperty("WireType", wireType);
                map.addProperty("PackageType", pkg);
                map.addProperty("RecipeID", etRecipeId.getText().toString().trim());
                NetUtils.requestGetNet(this, "EMS_GetRcpListByCondition", map, new FreeUI_DialogEntityCallBack<BaseResponseBean<SearchRecipeBean>>(
                        new TypeToken<BaseResponseBean<SearchRecipeBean>>() {
                        }.getType(), getSupportFragmentManager(), this) {
                    @Override
                    public void onSuccess(Response<BaseResponseBean<SearchRecipeBean>> response) {
                        if (response.body().isSuccess(getApplicationContext(), true,getSupportFragmentManager())) {
                            recipeSearchListAdapter.setNewData(response.body().getResponse().getBody().getRecipeList());
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<SearchRecipeBean>> response) {
                        super.onError(response);
                        uploadLog(response.getException());
                        loadError(response.getException(), "EMS_GetRcpListByCondition");
                    }
                });
                break;
            case R.id.llInfoTitle:
                if (llInfo.getVisibility() == View.VISIBLE) {
                    llInfo.setVisibility(View.GONE);
                    arrowAnimation(ivInfoArrow, 1);
                } else {
                    llInfo.setVisibility(View.VISIBLE);
                    arrowAnimation(ivInfoArrow, 2);
                }
                break;
            case R.id.ivEqpTypeArrow:
                if (eqpTypePop != null) {
                    if (eqpTypePop.isShowing()) {
                        eqpTypePop.dismiss();
                    } else {
                        FreeApi_PopUtils.setArrowShow(ivEqpTypeArrow);
                        eqpTypePop.showAsDropDown(flEqpType, 0, 0);
                    }
                }else{
                    eqpTypePop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flEqpType.getWidth(), eqpTypeList, ivEqpTypeArrow, (s, i) -> {
                        tvEqpType.setText(s);
                    });
                    FreeApi_PopUtils.setArrowShow(ivEqpTypeArrow);
                    eqpTypePop.showAsDropDown(flEqpType, 0, 0);
                }
                break;
            case R.id.ivWireTypeArrow:
                if (wireTypePop != null) {
                    if (wireTypePop.isShowing()) {
                        wireTypePop.dismiss();
                    } else {
                        FreeApi_PopUtils.setArrowShow(ivWireTypeArrow);
                        wireTypePop.showAsDropDown(flWireType, 0, 0);
                    }
                }else{
                    wireTypePop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flWireType.getWidth(), wireTypeList, ivWireTypeArrow, (s, i) -> {
                        tvWireType.setText(s);
                    });
                    FreeApi_PopUtils.setArrowShow(ivWireTypeArrow);
                    wireTypePop.showAsDropDown(flWireType, 0, 0);
                }
                break;
            case R.id.ivWireLongArrow:
                if (wireLengthPop != null) {
                    if (wireLengthPop.isShowing()) {
                        wireLengthPop.dismiss();
                    } else {
                        FreeApi_PopUtils.setArrowShow(ivWireLongArrow);
                        wireLengthPop.showAsDropDown(flWireLong, 0, 0);
                    }
                }else {
                    wireLengthPop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flWireLong.getWidth(), wireDiamList, ivWireLongArrow, (s, i) -> {
                        tvWireLong.setText(s);
                    });
                    FreeApi_PopUtils.setArrowShow(ivWireLongArrow);
                    wireLengthPop.showAsDropDown(flWireLong, 0, 0);
                }
                break;
            case R.id.ivPkgArrow:
                if (pkgPop != null) {
                    if (pkgPop.isShowing()) {
                        pkgPop.dismiss();
                    } else {
                        FreeApi_PopUtils.setArrowShow(ivPkgArrow);
                        pkgPop.showAsDropDown(flPkg, 0, 0);
                    }
                }else{
                    pkgPop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flPkg.getWidth(), peDownList, ivPkgArrow, (s, i) -> {
                        tvPkg.setText(s);
                    });
                    FreeApi_PopUtils.setArrowShow(ivPkgArrow);
                    pkgPop.showAsDropDown(flPkg, 0, 0);
                }
                break;
        }
    }
}
